package utils;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import dao.impl.DBUtil;

public class DBSelect {
	private static Logger logger = LogManager.getLogger(DBSelect.class);
	private String sql;
	private Object[] params;

	public DBSelect(String sql, Object... params) {
		this.sql = sql;
		this.params = new Object[params.length];
		for (int i = 0; i < params.length; i++) {
			this.params[i] = params[i];
		}
	}

	public void exec(DBSelectInt ds) {
		ResultSet rs = DBUtil.query(sql, this.params);
		if (null != ds) {
			try {
				ds.deal(rs);
			} catch (SQLException e) {
				logger.error(Pub.STACK_TRACE, e);
			}
		}
		DBUtil.release(rs);
	}
	public void execProcedure(DBSelectInt ds) {
		ResultSet rs = DBUtil.callProcedure(sql, this.params);
		if (null != ds) {
			try {
				ds.deal(rs);
			} catch (SQLException e) {
				logger.error(Pub.STACK_TRACE, e);
			}
		}
		DBUtil.release(rs);
	}
}
